package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 626
 * @description
 * @since 2022/05/19 15:08
 */

@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        Integer page = request.getPage();
        Integer size = request.getSize();
        String sort = request.getSort();
        Long userId = request.getUserId();

        OrderListResponse orderListResponse = new OrderListResponse();
        List<OrderDetailInfo> detailInfoList = null;
        long total = 0;
        try {
            // 默认下单时间倒序排序
            Example example = new Example(Order.class);
            example.createCriteria().andEqualTo("userId", userId).andNotEqualTo("status", 5);
            example.setOrderByClause("create_time" + " " + "desc");
            List<Order> orders = orderMapper.selectByExample(example);
            detailInfoList = new ArrayList<>();
            for (Order order : orders) {
                String orderId = order.getOrderId();
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

                List<OrderItemDto> orderItemDto = getOrderItemDtos(orderId);

                OrderShippingDto orderShippingDto = getOrderShippingDto(orderId);

                orderDetailInfo.setOrderItemDto(orderItemDto);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);
                detailInfoList.add(orderDetailInfo);
            }

            // 分页
            PageHelper.startPage(page, size);
            PageInfo<OrderDetailInfo> pageInfo = new PageInfo<>(detailInfoList);
            total = pageInfo.getTotal();

            orderListResponse.setDetailInfoList(detailInfoList);
            orderListResponse.setTotal(total);
            orderListResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderListResponse.setMsg(OrderRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("OrderCoreServiceImpl.orderList Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(orderListResponse, e);
        }

        return orderListResponse;
    }

    private OrderShippingDto getOrderShippingDto(String orderId) {
        Example orderShippingExample = new Example(OrderShipping.class);
        orderShippingExample.createCriteria().andEqualTo("orderId", orderId);
        OrderShipping orderShipping = orderShippingMapper.selectOneByExample(orderShippingExample);
        return orderConverter.shipping2dto(orderShipping);
    }

    private List<OrderItemDto> getOrderItemDtos(String orderId) {
        Example orderItemExample = new Example(OrderItem.class);
        orderItemExample.createCriteria().andEqualTo("orderId", orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
        return orderConverter.item2dto(orderItems);
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {

        String orderId = request.getOrderId();

        OrderDetailResponse response = new OrderDetailResponse();
        try {
            Example example = new Example(Order.class);
            example.createCriteria().andEqualTo("orderId", orderId);
            Order order = orderMapper.selectOneByExample(example);

            OrderShippingDto orderShippingDto = getOrderShippingDto(orderId);
            List<OrderItemDto> orderItemDto = getOrderItemDtos(orderId);

            response.setUserId(order.getUserId());
            response.setUserName(order.getBuyerNick());
            response.setTel(orderShippingDto.getReceiverPhone());
            response.setStreetName(orderShippingDto.getReceiverAddress());
            response.setOrderTotal(order.getPayment());
            response.setOrderStatus(order.getStatus());
            response.setGoodsList(orderItemDto);

            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("OrderCoreServiceImpl.orderDetail Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }


        return response;
    }
}
